package net.app.zoneland.file.utils;

import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import net.app.zoneland.file.properties.MinIoProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Description 文件工具类
 * @Author cjb
 * @CreateTime 2021/1/1 1:47
 * @Version: 1.0
 */
@Component
@Configuration
@EnableConfigurationProperties({MinIoProperties.class})
public class MinIoUtils {

    private MinIoProperties minIo;

    private MinioClient instance;

    public MinIoUtils(MinIoProperties minIo) {
        this.minIo = minIo;
    }

    @PostConstruct
    public void init() {
        try {
            instance = new MinioClient(minIo.getEndpoint(),minIo.getAccessKey(),minIo.getSecretKey());
        } catch (InvalidPortException e) {
            e.printStackTrace();
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断 bucket是否存在
     * @param bucketName
     * @return
     */
    public boolean bucketExists(String bucketName){
        try {
            return instance.bucketExists(bucketName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 创建 bucket
     * @param bucketName
     */
    public boolean makeBucket(String bucketName){
        boolean isExist;
        try {
               isExist = instance.bucketExists(bucketName);
            if(!isExist) {
                instance.makeBucket(bucketName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isExist=false;
        }
        return isExist;
    }

    /**
     * 文件上传
     * @param bucketName
     * @param objectName
     * @param filename
     */
    public String putObject(String bucketName, String objectName, String filename,PutObjectOptions options) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException, InvalidExpiresRangeException {
                       instance.putObject(bucketName,objectName,filename,options);
          String url = instance.getObjectUrl(bucketName, objectName);
          return url;
    }

    /**
     * 文件上传
     * @param bucketName
     * @param objectName
     * @param stream
     */
    public String putObject(String bucketName, String objectName, InputStream stream,PutObjectOptions options){
        String url = null;
        try {
            instance.putObject(bucketName,objectName,stream,options);
              url = instance.getObjectUrl(bucketName, objectName);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * 删除文件
     * @param bucketName
     * @param objectName
     */
    public void removeObject(String bucketName, String objectName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
            instance.removeObject(bucketName,objectName);
    }

}
